package leetCode.firstWeek;

public class Code_42 {
  public int trap(int[] height) {
    int total_former = 0;
    for (int i = 0; i < height.length; i++) {
      total_former += height[i];
    }

    int[] tmp = height;
    int max = 0, max_index = 0;
    for (int i = 0; i < tmp.length; i++) {
      if (tmp[i] > max) {
        max = tmp[i];
        max_index = i;
      }
    }

    for (int i = 0; i < tmp.length; i++) {
      if (i == max_index) {
        continue;
      }
      int start = i > max_index ? max_index : i;
      int end = i > max_index ? i : max_index;

      for (int j = start; j < end; j++) {
        if (tmp[j] < tmp[i]) {
          tmp[j] = tmp[i];
        }
      }
    }

    int total_afer = 0;
    for (int i = 0; i < tmp.length; i++) {
      total_afer += tmp[i];
    }

    return total_afer - total_former;
  }
}
